Counting the last of word in a string.

input: "Hello World"
output: 5

input: " fly me to the moon "
output: 4

input: "luffy is still joyboy"
output: 6

思路

從字串的最後倒著數,
設一個counter去數字,
並設一個變數(doStartCounting)確認字串最後是不是空格(ascii為32),
一旦開始數字串(string)裡的字元(char)時,也就是counter不為0時,
確認空格的變數(doStartCounting)轉為1(True), 再次碰見空白字元或迭代結束時,
就跳出迭代(iteration)(for loop),
並回傳counter。

code(cpp)


problem link

leetcode